app.controller('OrderAcceptCtrl', function($scope,$state,$ionicPlatform,$ionicLoading,$cordovaBarcodeScanner,$cordovaGeolocation,appFactory,$User,$filter) {

    $ionicPlatform.ready(function(){
        $scope.urole = $User.get('role');
    });

    // 获取甩单列表
    $scope.getTransfer = function(){
        $state.go('tab.getTransfer') ;
    };

    // 获取甩单列表
    $scope.getTransferExceute = function(){
        $ionicLoading.show({
            template: '查询中...'
        });
        $scope.transferOrdersArray = [];
        param = {delflag:0,uphone:0};
        appFactory.getTransferOrder(param).then(function(data){
            $ionicLoading.hide();
            $scope.transferOrdersArray = data;
        }).catch(function(err){
            $ionicLoading.hide();
        });
    };
    $scope.getTransferExceute();

    $scope.getTransferOrders = function(uphone){
        if(uphone===undefined || uphone.length<6){
            alert("请输入至少6位连续的用户手机号");
            $scope.$broadcast('scroll.refreshComplete');
            return;
        }
        $ionicLoading.show({
            template: '查询中...'
        });
        $scope.transferOrdersArray = [];
        param = {delflag:0,uphone:uphone};
        appFactory.getTransferOrder(param).then(function(data){
            $ionicLoading.hide();
            $scope.transferOrdersArray = data;
        }).catch(function(err){
            $ionicLoading.hide();
        });
    };

    $scope.getOrdersArray = function(mhOid){
        if(mhOid===undefined || mhOid.length<6){
            alert("请输入至少6位连续的订单编号");
            $scope.$broadcast('scroll.refreshComplete');
            return;
        }
        $ionicLoading.show({
            template: '查询中...'
        });
        $scope.ordersArray = [];
        param = {orderid:mhOid,uid:$User.get("id")};
        appFactory.findOrder(param).then(function(data){
            $scope.ordersArray = data;
            $ionicLoading.hide();
        }).catch(function(err){
            if(err.errno === -46){
                alert("LBS 错误");
            }
            if(err.errno === -44){
                alert('订单不存在或者订单在非可领取状态，请核实！');
            }
            $ionicLoading.hide();
        });
    }
    $scope.acceptOrder = function($index,$type){
        if(!confirm("确认领取订单？")){
            return false;
        }
        $ionicLoading.show({});
        var posOptions = {timeout: 10000, enableHighAccuracy: false};
        if($type=='order'){
            order = $scope.ordersArray[$index];
        }else{
            order = $scope.transferOrdersArray[$index];
        }
        $cordovaGeolocation.getCurrentPosition(posOptions).then(function (position) {
            lat = position.coords.latitude;
            long = position.coords.longitude;
            param = {orderid:order.orderid,ordertype:order.type,uid:$User.get("id"),lat:lat,long:long};
            appFactory.acceptOrder(param).then(function (d) {
                if($type=='order'){
                    $scope.ordersArray.splice($index,1);
                }else{
                    $scope.transferOrdersArray.splice($index,1);
                }
                $ionicLoading.hide();
                alert("领单成功，请到任务查看");
                if(order.expresstype == '定时送达'){
                    var time = $filter('date')(order.deliveryTime*1000,'yyyy-MM-dd HH:mm');
                    alert("注意：此订单为定时送达订单，用户预约的时间为: "+time+" 请留心安排时间哦！");
                }
            }).catch(function(err){
                $ionicLoading.hide();
                switch (err.errno){
                    case -41:
                        //订单状态非可以领件状态
                        alert("该订单无法被领取，订单非可领件状态！");
                        break;
                    case -42:
                        alert("该订单已被领取");
                        break;
                    case -43:
                        //订单已存在，不能重复领取
                        alert("问题订单，请反馈！！");
                        break;
                    case -44:
                        //订单不存在
                        alert("该订单无法领取，未搜索到该订单。请反馈！！");
                        break;
                    case -45:
                        alert("该订单不在可领件范围内！！");
                        break;
                    default :
                        alert("重新尝试下，若依旧弹出该窗口，请检查网络，并联系技术部！");
                        break;
                }
            });
        }).catch(function(err){
            alert('获取定位信息失败，请开启定位服务！')
        });
    }
    $scope.barscan = function(){
        $cordovaBarcodeScanner
            .scan()
            .then(function(barcodeData) {
                var cancelled = barcodeData.cancelled;
                var result = JSON.parse(barcodeData.text);
                if(!cancelled){
                    var oid = result.OID;
                    if(oid){
                        $scope.getOrdersArray(oid)
                    }else{
                        alert('没有获取到oid');
                    }
                }else{
                    alert('扫描异常或退出扫描');
                }
            }, function(error) {
                // An error occurred
                alert('扫描异常或退出扫描');
            });
    };

    //扫码派送快件
    $scope.barscanExpress1 = function(){
        $cordovaBarcodeScanner
            .scan()
            .then(function(barcodeData) {
                var cancelled = barcodeData.cancelled;
                var result = JSON.parse(barcodeData.text);
                if(!cancelled){
                    $scope.getExpress1(result);
                }else{
                    var r = confirm('扫描异常或退出扫描!\r\n是否继续扫码?');
                    if(r===true){
                        $scope.barscanExpress1();
                    }
                }
            }, function(error) {
                // An error occurred
                var r = confirm('扫描异常或退出扫描!\r\n是否继续扫码?');
                if(r===true){
                    $scope.barscanExpress1();
                }
            });
    };

    $scope.getExpress1 = function(mhOid){
        if(mhOid===undefined || mhOid.length<6){
            alert("请输入后6位连续的快件单号");
            $scope.$broadcast('scroll.refreshComplete');
            return;
        }

        param = {expressno:mhOid,uid:$User.get("id"),area:$User.get("area")};
        appFactory.getExpress1(param).then(function(data){
            alert(data.msg);
            var r = confirm('是否继续扫码?');
            if(r===true){
                $scope.barscanExpress1();
            }
        }).catch(function(err){
            var r = confirm('扫描异常!\r\n是否继续扫码?');
            if(r===true){
                $scope.barscanExpress1();
            }
        });
    }

    //扫码领取快件至站点
    $scope.barscanExpress2 = function(){
        $cordovaBarcodeScanner
            .scan()
            .then(function(barcodeData) {
                var cancelled = barcodeData.cancelled;
                var result = JSON.parse(barcodeData.text);
                if(!cancelled){
                    $scope.getExpress2(result);
                }else{
                    var r = confirm('扫描异常或退出扫描!\r\n是否继续扫码?');
                    if(r===true){
                        $scope.barscanExpress2();
                    }
                }
            }, function(error) {
                // An error occurred
                var r = confirm('扫描异常或退出扫描!\r\n是否继续扫码?');
                if(r===true){
                    $scope.barscanExpress2();
                }
            });
    };

    $scope.getExpress2 = function(mhOid){
        if(mhOid===undefined || mhOid.length<6){
            alert("请输入后6位连续的快件单号");
            $scope.$broadcast('scroll.refreshComplete');
            return;
        }

        param = {expressno:mhOid,uid:$User.get("id"),area:$User.get("area")};
        appFactory.getExpress2(param).then(function(data){
            alert(data.msg);
            var r = confirm('是否继续扫码?');
            if(r===true){
                $scope.barscanExpress2();
            }
        }).catch(function(err){
            var r = confirm('扫描异常!\r\n是否继续扫码?');
            if(r===true){
                $scope.barscanExpress2();
            }
        });
    }

    //扫码查单，查询订单状态
    $scope.barscanQuery = function(){
        $cordovaBarcodeScanner
            .scan()
            .then(function(barcodeData) {
                var cancelled = barcodeData.cancelled;
                var result = JSON.parse(barcodeData.text);
                if(!cancelled){
                    $scope.getOrderStatus(result.OID);
                }else{
                    var r = confirm('扫描异常或退出扫描!\r\n是否继续扫码?');
                    if(r===true){
                        $scope.barscanQuery();
                    }
                }
            }, function(error) {
                // An error occurred
                var r = confirm('扫描异常或退出扫描!\r\n是否继续扫码?');
                if(r===true){
                    $scope.barscanQuery();
                }
            });
    };

    $scope.getOrderStatus = function(mhOid){
        if(mhOid===undefined || mhOid.length<6){
            alert("请输入6位连续的订单单号");
            $scope.$broadcast('scroll.refreshComplete');
            return;
        }

        param = {orderid:mhOid};
        appFactory.getOrderStatus(param).then(function(data){
            alert(data.msg);
            var r = confirm('是否继续扫码?');
            if(r===true){
                $scope.barscanQuery();
            }
        }).catch(function(err){
            var r = confirm('扫描异常!\r\n是否继续扫码?');
            if(r===true){
                $scope.barscanQuery();
            }
        });
    }
});
